﻿using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using INHollandair.Core.Models;
using INHollandair.Core.Services;

namespace INHollandair.Shop
{
    public partial class Products : System.Web.UI.Page
    {
        private readonly ProductService _svc = new ProductService();

        protected void Page_Load(object sender, EventArgs e)
        {
            EmptyListLabel.Text = String.Empty;
            errorLabel.Text = String.Empty;
            errorLabel.Visible = false;

            if (!IsPostBack)
            {
                GetAllProducts();
            }
        }

        private void SearchProducts()
        {
            DateTime startDate;
            String input = Server.HtmlEncode(FromDate.Text);

            //Check if input is valid
            if (DateTime.TryParse(input, out startDate))
            {
                //Reload datagrid with start date
                if (startDate.Date >= DateTime.Now.Date && startDate.Date.Year <= DateTime.Now.AddYears(1).Date.Year)
                {
                    List<Product> products = _svc.GetProductsByDate(startDate);
                    ProductGrid.DataSource = products;
                    ProductGrid.DataBind();

                    if(products.Count.Equals(0))
                    {
                        EmptyListLabel.Text = String.Format("Er zijn geen vluchten gevonden voor {0}.", input);
                    }
                }
                else
                {
                    FromDate.Text = DateTime.Now.Date.ToShortDateString();
                    errorLabel.Text = "Ongeldige datum ingevoerd, probeer het opnieuw.";
                    errorLabel.Visible = true;
                }
            }
            else
            {
                errorLabel.Text = "Er is een fout opgetreden, probeer het opnieuw.";
                errorLabel.Visible = true;
            }
            SearchHeaderLabel.Text = String.Format("Zoek resultaten voor {0}.", input);
        }

        protected void GetAllProducts()
        {
            List<Product> products = _svc.GetProducts();
            ProductGrid.DataSource = products;
            ProductGrid.DataBind();
            SearchHeaderLabel.Text = "Alle producten";
            if (products.Count.Equals(0))
            {
                EmptyListLabel.Text = "Er zijn geen vluchten gevonden.";
            }
        }

        protected void ProductGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if(e.CommandName == "ShowProduct") 
            {
                Response.Redirect("~/Shop/ShowProduct.aspx?id=" + e.CommandArgument);
            }
        }
        
        protected void ProductSearchButtonClick(object sender, EventArgs e)
        {
            SearchProducts();
        }

        protected void ProductSearchResetButtonClick(object sender, EventArgs e)
        {
            FromDate.Text = String.Empty;
            GetAllProducts();
        }
    }
}